import { IEchartsData } from '@/model/echarts/echartsInter'
import { dataFlag, noSeries, sortObj } from '@/utils/echartsCommonFun'
import {
  EChartsOption,
  PieSeriesOption
} from 'echarts/types/dist/shared'

export function Chart (datas:IEchartsData) {
  const series = []
  const flag = dataFlag(datas.data)
  if (flag > 0 && flag === datas.data.length) { // 没有数据
    series.push(noSeries())
  } else {
    datas.data.forEach(data => {
      let value:Array<{name:string, value:number}> = []
      if (data.value) {
        value = data.value
      }
      value = sortObj(value)
      series.push({
        name: data.name ? data.name : '',
        type: 'pie',
        radius: ['65%', '80%'],
        label: {
          show: false,
          fontSize: '44',
          fontWeight: 'normal',
          color: '#fff',
          position: 'center',
          formatter: '{b}' + ':' + '{c}'
        },
        labelLine: {
          show: false
        },
        emphasis: {
          label: {
            show: true,
            formatter: '{d}' + '%'
          }
        },
        selectedOffset: 5,
        data: value
      } as PieSeriesOption)
    })
  }
  return {
    color: ['#F9C20A', '#009A9F', '#77C14C', '#FFC27C', '#F58025'],
    title: [{
      text: '',
      x: 'center',
      y: 'center',
      textStyle: {
        fontSize: '44',
        fontWeight: 'normal',
        color: '#fff',
        textAlign: 'center'
      }
    }],
    series: series
  } as EChartsOption
}
